package com.aijia.custom.product.ui;

import android.graphics.Rect;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.KeyEvent;

import com.aijia.custom.product.R;
import com.aijia.custom.product.fragment.MyFragment;
import com.aijia.custom.product.fragment.OrderFragment;
import com.aijia.custom.product.utils.Notification;
import com.lhh.apst.library.APSTSViewPager;
import com.lhh.apst.library.AdvancedPagerSlidingTabStrip2;

public class MainActivity extends FragmentActivity {
    public AdvancedPagerSlidingTabStrip2 mAPSTS;
    public APSTSViewPager mVP;

    private static final int VIEW_FIRST = 0;
    private static final int VIEW_SECOND = 1;

    private final int VIEW_SIZE = 2;

    private Fragment firstFragment;
    private MyFragment myFragment;
    private int userType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userType = getIntent().getIntExtra("user_type", 1);
        initApst();

    }

    private void initApst() {
        mAPSTS = findViewById(R.id.main_tabs);
        mVP = findViewById(R.id.main_apsts);

        mVP.setOffscreenPageLimit(VIEW_SIZE);
        mVP.setNoFocus(true);
        mVP.setCurrentItem(VIEW_FIRST);

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
        mVP.setAdapter(adapter);
        mAPSTS.setViewPager(mVP);
    }


    private class FragmentAdapter extends FragmentStatePagerAdapter implements AdvancedPagerSlidingTabStrip2.IconTabProvider {

        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_FIRST:
                        if (null == firstFragment) {
                            firstFragment = OrderFragment.instance();
                        }
                        return firstFragment;
                    case VIEW_SECOND:
                        if (null == myFragment)
                            myFragment = MyFragment.instance();
                        return myFragment;
                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public int getCount() {
            return VIEW_SIZE;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if (position >= 0 && position < VIEW_SIZE) {
                switch (position) {
                    case VIEW_FIRST:
                        return "订单";
                    case VIEW_SECOND:
                        return "我的";
                    default:
                        break;
                }
            }
            return null;
        }

        @Override
        public Integer getPageIcon(int index) {
            if (index >= 0 && index < VIEW_SIZE) {
                switch (index) {
                    case VIEW_FIRST:
                        return R.mipmap.main_tab_order_nor;
                    case VIEW_SECOND:
                        return R.mipmap.main_tab_my_nor;
                    default:
                        break;
                }
            }
            return 0;
        }

        @Override
        public Integer getPageSelectIcon(int index) {
            if (index >= 0 && index < VIEW_SIZE) {
                switch (index) {
                    case VIEW_FIRST:
                        return R.mipmap.main_tab_order_pre;
                    case VIEW_SECOND:
                        return R.mipmap.main_tab_my_pre;
                    default:
                        break;
                }
            }
            return 0;
        }

        @Override
        public Rect getPageIconBounds(int position) {
            return null;
        }

    }

    private long firstTime;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 800) {// 如果两次按键时间间隔大于800毫秒，则不退出
                Notification.toast(MainActivity.this, "再按一次退出程序");
                firstTime = secondTime;// 更新firstTime
                return true;
            } else {
                this.finish();
            }
        }

        return super.onKeyUp(keyCode, event);
    }


}



